//
//  ViewController.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView1;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 每次程序载入后读取以前的UserDefaults
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    NSString *str = [d objectForKey:@"number"];
    self.textView1.text = str;
    // 判断第一次是否为空
    if ([self.textView1.text isEqual: @""]) {
        NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
        NSString *date = @"";
        [d setObject:date forKey:@"number"];
        [d synchronize];
    }
   

    // 程序载入后
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo1) name:UIApplicationDidFinishLaunchingNotification object:nil];
    // 程序从后台回到前台
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo2) name:UIApplicationWillEnterForegroundNotification object:nil];
    // 监听程序将要进入前台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo) name:UIApplicationWillEnterForegroundNotification object:nil];
    // 程序已经进入后台
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo3) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSLog(@"%s",__func__);
    // 3. 第二个页面回来的时候，更新下界面信息
    [self updateInfo];
    [self updateInfo1];
    [self updateInfo2];
    [self updateInfo3];
    [self setUpBackground];
}

-(void)setUpBackground{
    NSUserDefaults *m = [NSUserDefaults standardUserDefaults];
    float red = [[m valueForKey:@"redSlider"] floatValue];
    float green = [[m valueForKey:@"greenSlider"] floatValue];
    float blue = [[m valueForKey:@"blueSlider"] floatValue];
    self.textView1.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSString *)time
{
    NSDate *date = [NSDate date];
    NSDateFormatter *f = [[NSDateFormatter alloc]init];
    [f setDateFormat:@"HH:mm:ss"];
    NSString *str = [f stringFromDate:date];
    return str;
}
- (void)updateInfo3
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    NSString *str = [d objectForKey:@"number"];
    NSString *add = [[NSString stringWithFormat:@"%@ 程序已经进入后台\n",[self time]] stringByAppendingString:str];
    self.textView1.text = add;
    [d setObject:add forKey:@"number"];
    [d synchronize];
}
- (void)updateInfo2
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    NSString *str = [d objectForKey:@"number"];
    NSString *add = [[NSString stringWithFormat:@"%@ 程序从后台回到前台\n",[self time]] stringByAppendingString:str];
    self.textView1.text = add;
    [d setObject:add forKey:@"number"];
    [d synchronize];
}
- (void)updateInfo1
{
        NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
        NSString *str = [d objectForKey:@"number"];
        NSString *add = [[NSString stringWithFormat:@"%@ 程序载入后\n",[self time]] stringByAppendingString:str];
        
        self.textView1.text = add;
        [d setObject:add forKey:@"number"];
        [d synchronize];
}
- (void)updateInfo
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    NSString *str = [d objectForKey:@"number"];
    NSString *add = [[NSString stringWithFormat:@"%@ 将要进入前台\n",[self time]] stringByAppendingString:str];
    self.textView1.text = add;
    [d setObject:add forKey:@"number"];
    [d synchronize];
}

@end
